package com.huichaoliu.stylist.activity;

import java.util.ArrayList;
import java.util.List;

import com.huichaoliu.adapters.FragmentSosAdapter;
import com.huichaoliu.app.R;
import com.huichaoliu.data.UtilInfo;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class StylistSosFragment extends Fragment implements OnClickListener,
		OnItemClickListener {
	// tools
	Context context;

	// Views
	private View v = null;
	TextView leftBtn;
	TextView rightBtn;

	ListView listLeft;
	ListView listRight;

	// adapter
	FragmentSosAdapter leftAdapter;
	FragmentSosAdapter rightAdapter;

	// list
	List<UtilInfo> listInfoLeft;
	List<UtilInfo> listInfoRight;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (v == null) {
			v = inflater.inflate(R.layout.fragment_stylist_sos, container,
					false);
		}
		ViewGroup parent = (ViewGroup) v.getParent();
		if (parent != null) {
			parent.removeView(v);
		}
		context = getActivity();
		init();
		getLeftData();
		getRightData();
		return v;
	}

	private void init() {
		leftBtn = (TextView) v.findViewById(R.id.sos_left);
		listLeft = (ListView) v.findViewById(R.id.sos_list_left);

		rightBtn = (TextView) v.findViewById(R.id.sos_right);
		listRight = (ListView) v.findViewById(R.id.sos_list_right);

		leftBtn.setOnClickListener(this);
		rightBtn.setOnClickListener(this);

		listLeft.setOnItemClickListener(this);
		listRight.setOnItemClickListener(this);

		changePosition(0);
	}

	private void getLeftData() {
		listInfoLeft = new ArrayList<UtilInfo>();
		for (int i = 0; i < 20; i++) {
			listInfoLeft.add(new UtilInfo());
		}
		leftAdapter = new FragmentSosAdapter(listInfoLeft, context);
		listLeft.setAdapter(leftAdapter);

	}
	
	private void getRightData() {
		listInfoRight = new ArrayList<UtilInfo>();
		for (int i = 0; i < 20; i++) {
			listInfoRight.add(new UtilInfo());
		}
		rightAdapter = new FragmentSosAdapter(listInfoRight, context);
		listRight.setAdapter(rightAdapter);

	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.sos_left:
			changePosition(0);
			break;
		case R.id.sos_right:
			changePosition(1);
			break;
		default:
			break;
		}
	}

	@SuppressWarnings("deprecation")
	private void changePosition(int code) {
		switch (code) {
		case 0:
			leftBtn.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.stylist_showpic_n));
			rightBtn.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.stylist_showpic_p));
			listLeft.setVisibility(View.VISIBLE);
			listRight.setVisibility(View.GONE);
			break;
		case 1:
			leftBtn.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.stylist_showpic_p));
			rightBtn.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.stylist_showpic_n));
			listLeft.setVisibility(View.GONE);
			listRight.setVisibility(View.VISIBLE);
			break;

		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent(context, StylistSosDetailActivity.class);
		startActivity(intent);
		// switch (arg0.getId()) {
		// case R.id.sos_list_left:
		//
		// break;
		// case R.id.sos_list_right:
		//
		// break;
		//
		// default:
		// break;
		// }
	}
}
